package com.demo0302.servlet;

import com.demo0302.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/demo0302/demo05.do")
public class Demo05Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.模拟从数据库中取出的 list
        List<User> users = new ArrayList<>();
        users.add(new User(1, "张三", 19));
        users.add(new User(2, "李四", 20));
        users.add(new User(3, "王五", 21));
        users.add(new User(4, "小明", 22));
        users.add(new User(5, "小强", 23));

        //2.将需要展示的数据放到 request 域对象中
        request.setAttribute("users", users);
        //3.将请求对象转发到对应的 Servlet（JSP 也是一个特殊的 Servlet） 中
        request.getRequestDispatcher("./userList.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
